/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PuriON;

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.StringTokenizer;

/**
 *
 * @author jjangg96
 */
public class PURION_Connect {

    Socket sock = null;
    private String ip;
    private int port;
    private JFrame parent;

    PURION_Connect(JFrame frame) {
        
        loadSettingFile("./setting");
        
        parent = frame;
        try {
            sock = new Socket(ip, port);
            sock.setSoTimeout(5000);
        } catch (ConnectException ex) {
            System.out.println(ip + "," + port + "; 접속할 수 없습니다.");
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }
    public void loadSettingFile(String fn) {        
        int i = 0;
        InputStream in = null;
        try {
            in = new FileInputStream(new File(fn));
        } catch (FileNotFoundException e) {
            System.out.println(e);
            return;
        } catch (IOException e) {
            System.out.println(e);
        }
        try {
            String temp = "";
            while ((i = in.read()) != -1) {       
                temp = temp + new Character((char) i);
            }
            StringTokenizer token = new StringTokenizer(temp, "|");            
            ip = token.nextToken();
            port = new Integer(token.nextToken()).intValue();
            in.close();
        } catch (IOException e1) {
            System.out.println(e1);
        }
        System.err.println("PURION_Connected : ./setting is loaded");
    }
    private void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    private void showAlert() {
        JOptionPane.showMessageDialog(parent, "서버가 작동하지 않거나 인터넷 접속이 되어있지 않습니다.");
    }
    //08.05.30 이진우 PURION_Connect 추가 부분
    //네트워크 작동중 오류 발생 표시 부분
    public void showAlert(String message) {
        JOptionPane.showMessageDialog(parent, "네트워크 오류\n" + message);
    }

    //08.05.30 이진우 PURION_Connect 추가 부분
    //자기 아이피를 받는다.
    public String getMyIP() {
        return sock.getInetAddress().getHostAddress();
    }

    //08.05.30 이진우 PURION_Connect 수정 부분
    //기존의 boolean 리턴에서 받은 메시지를 return하게 하였다.
    public String sendMessage(String message) {
        String ret = null;

        try {
            if (sock.isConnected()) {
                BufferedReader br;
                PrintWriter pw = new PrintWriter(sock.getOutputStream(), true);

                br = new BufferedReader(new InputStreamReader(sock.getInputStream()));

                pw.println(message);

                String str = br.readLine();

                delay(1000);

                // 리턴 문자가 널이거나 빈문자면..
                if (str == null | str.trim().equals("")) {

                    showAlert();
                } else {
                    ret = str;

                }
            } else {
                showAlert();
            }
            sock.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {

            return ret;
        }
    }
}